package com.yh.correct.controller;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.yh.correct.model.ResultData;
import com.yh.correct.model.SysPermission;
import com.yh.correct.model.SysRole;
import com.yh.correct.model.vo.Tree;
import com.yh.correct.service.ISysPermissionService;
import com.yh.correct.service.ISysRoleService;

/**
 * 角色管理控制层
 * 
 * @author Administrator
 *
 */
@RestController
@RequestMapping(value = "/sysrole")
public class SysRoleController {

	// 日志
	private static Logger logger = LogManager.getLogger(SysRoleController.class);

	// 角色服务层接口
	@Autowired
	private ISysRoleService roleService;

	// 权限服务层接口
	@Autowired
	private ISysPermissionService permissionService;

	/**
	 * 跳转到角色管理页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/show")
	public ModelAndView toShow() {
		ModelAndView mv = new ModelAndView("/system/sysrole");
		return mv;
	}

	/**
	 * 动态查找角色分页
	 * @param roleName
	 * @param pageNum
	 * @param pageSize
	 * @return
	 */
	@RequestMapping(value = "/search")
	public ResultData searchRole(String roleName, Integer pageNum, Integer pageSize) {
		Map<String, Object> condition = new HashMap<String, Object>();
		condition.put("pageNum", pageNum);
		condition.put("pageSize", pageSize);
		condition.put("roleName", roleName);
		PageInfo<Object> pageInfo = null;
		try {
			pageInfo = roleService.findListByCondition(condition);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		List<Object> list = new ArrayList<Object>();
		list.add(pageInfo);
		ResultData rd = new ResultData();
		rd.setCode(0);
		rd.setMessage("查询成功！");
		rd.setData(list);
		return rd;
	}

	
	/**
	 * 跳转到增加角色页面
	 * @return
	 */
	@RequestMapping(value = "/toAdd")
	public ModelAndView toAddRole() {
		List<Tree> permissions = null;
		try {
			permissions = permissionService.findTreeList();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ModelAndView mv = new ModelAndView("/system/sysrole_add");
		mv.addObject("permissions", permissions);
		return mv;
	}

	/**
	 * 增加角色
	 * @param role
	 * @param strArr
	 * @return
	 */
	@RequestMapping(value = "/doAdd")
	public ResultData doAddRole(SysRole role, String strArr) {
		//jack-json 把json字符串转成int数组
		ObjectMapper objectMapper = new ObjectMapper();
		Integer[] perArr = null;
		try {
			perArr = objectMapper.readValue(strArr, Integer[].class);
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int i = 0;
		try {
			role.setCreateTime(new Timestamp(System.currentTimeMillis()));
			i = roleService.insertSysRole(role, perArr);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ResultData rd = new ResultData();
		if (i>0) {
			rd.setCode(0);
			rd.setMessage("增加成功！");
			return rd;
		} else {
			rd.setCode(4001);
			rd.setMessage("增加失败！");
			return rd;
		}
	}
	
	/**
	 * 删除角色
	 * @param role
	 * @return
	 */
	@RequestMapping(value="/doDel")
	public ResultData doDelRole(SysRole role){
		int i=0;
		try {
			i=roleService.deleteSysRole(role);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ResultData rd = new ResultData();
		if (i>0) {
			rd.setCode(0);
			rd.setMessage("删除成功！");
			return rd;
		} else {
			rd.setCode(4001);
			rd.setMessage("删除失败！");
			return rd;
		}
		
	}
	
	/**
	 * 跳转到更新角色页面
	 * @param role
	 * @return
	 */
	@RequestMapping(value="/toEdit")
	public ModelAndView toEditRole(SysRole role){
		Map<String,Object> roleObj=null;
		try {
			roleObj=roleService.getSysRole(role);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ModelAndView mv=new ModelAndView("/system/sysrole_edit");
		mv.addObject("roleObj", roleObj);
		return mv;
	}
	
	/**
	 * 获取权限列表（含角色对象的权限状态）
	 * @param role
	 * @return
	 */
	@RequestMapping(value="/getRoleTree")
	public ResultData getRoleTree(SysRole role){
		Map<String,Object> roleObj=null;
		List<Tree> allList=null;
		try {
			roleObj=roleService.getSysRole(role);
			List<SysPermission> roleList=(List<SysPermission>) roleObj.get("permissions");
			allList=permissionService.findTreeList();
			for(Tree a:allList){
				for(SysPermission r:roleList){
					if(a.getId()==r.getId()){
						a.setChecked(true);
					}
				}
			}
			roleObj.put("permissions", allList);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ResultData rd = new ResultData();
		rd.setCode(0);
		rd.setMessage("查询成功！");
		rd.setData(allList);
		return rd;
	}
	
	/**
	 * 更新角色
	 * @param role
	 * @param strArr
	 * @return
	 */
	@RequestMapping(value = "/doEdit")
	public ResultData doEditRole(SysRole role, String strArr) {
		//jack-json 把json字符串转成int数组
		ObjectMapper objectMapper = new ObjectMapper();
		Integer[] perArr = null;
		try {
			perArr = objectMapper.readValue(strArr, Integer[].class);
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		int i = 0;
		try {
			role.setCreateTime(new Timestamp(System.currentTimeMillis()));
			i = roleService.updateSysRole(role, perArr);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ResultData rd = new ResultData();
		if (i>0) {
			rd.setCode(0);
			rd.setMessage("修改成功！");
			return rd;
		} else {
			rd.setCode(4001);
			rd.setMessage("修改失败！");
			return rd;
		}
	}
	
	
	/**
	 * 跳转到角色详细页面
	 * @param role
	 * @return
	 */
	@RequestMapping(value="/detail")
	public ModelAndView toDetailRole(SysRole role){
		Map<String,Object> roleObj=null;
		try {
			roleObj=roleService.getSysRole(role);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ModelAndView mv=new ModelAndView("/system/sysrole_detail");
		mv.addObject("roleObj", roleObj);
		return mv;
	}

}
